/*
 * DA05_7Seg.c
 *
 * Created: 4/6/2014 6:02:36 PM
 *  Author: mummm2
 */ 


/*
PB0->A
PB1->B
PB2->C
PB3->D
PB4->E
PB5->F
PB6->G
PB7->DP
*/

#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void){
	//7Segment numbers preset to send to portB
	int num1 = 6;
	int num2 = 91;
	int num3 = 79;
	int num4 = 102;
	int num5 = 109;
	int num6 = 124;
	int num7 = 7;
	int num8 = 127;
	int num9 = 103;
	int num0 = 63;
	
	DDRB = 0xff;	//set all of portB as output, goes to 7segment display
	DDRD = 0xff;	//set all of portD as output, used to turn on each 7segment individually
	
	//data will be L-Justified, PC5 for analog reading, 1.1V internal voltage ref with 10uf cap
	ADMUX = (1<<REFS1) | (1<<REFS0) | (1<<ADLAR) | (0<<MUX3) | (1<<MUX2) | (0<<MUX1) | (1<<MUX0);
	//make ADC enable and select ck/128
	ADCSRA = (1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
	
	
	int digit1;	//tens place digit for temperature 
	int digit2;	//ones place digit for temperature
	int digit3;	//decimal digit for temperature
	
	OCR1A = 0x7a12;			//Delay time, 1sec
	TCCR1A = (0<<WGM11) | (0<<WGM10);	//CTC
	TCCR1B = (0<<WGM13) | (1<<WGM12) | (1<<CS12) | (0<<CS11) | (0<<CS10);  //N=256
	
	while(1)
	{
		
		ADCSRA |= (1<<ADSC);			//Start Conversion
		while((ADCSRA&(1<<ADIF))==0);	//wait for end of conversion
		
		digit1=ADCH;				//capture ADC
		digit1 *= 4.444;			//due to low reference voltage, number needs to be mult
		
		digit3 = digit1%10;			//decimal place
		digit2 = (digit1/10)%10;	//ones place
		digit1 = (digit1/100)%10;	//tens place
		
		//Grabs the 7seg display number based on the real number
		if(digit1 == 9)
		digit1 = num9;
		else if (digit1 == 8)
		digit1 = num8;
		else if (digit1 == 7)
		digit1 = num7;
		else if (digit1 == 6)
		digit1 = num6;
		else if (digit1 == 5)
		digit1 = num5;
		else if (digit1 == 4)
		digit1 = num4;
		else if (digit1 == 3)
		digit1 = num3;
		else if (digit1 == 2)
		digit1 = num2;
		else if (digit1 == 1)
		digit1 = num1;
		else
		digit1 = num0;
		
		//Grabs the 7seg display number based on the real number
		if(digit2 == 9)
		digit2 = num9;
		else if (digit2 == 8)
		digit2 = num8;
		else if (digit2 == 7)
		digit2 = num7;
		else if (digit2 == 6)
		digit2 = num6;
		else if (digit2 == 5)
		digit2 = num5;
		else if (digit2 == 4)
		digit2 = num4;
		else if (digit2 == 3)
		digit2 = num3;
		else if (digit2 == 2)
		digit2 = num2;
		else if (digit2 == 1)
		digit2 = num1;
		else
		digit2 = num0;
		
		//Grabs the 7seg display number based on the real number
		if(digit3 == 9)
		digit3 = num9;
		else if (digit3 == 8)
		digit3 = num8;
		else if (digit3 == 7)
		digit3 = num7;
		else if (digit3 == 6)
		digit3 = num6;
		else if (digit3 == 5)
		digit3 = num5;
		else if (digit3 == 4)
		digit3 = num4;
		else if (digit3 == 3)
		digit3 = num3;
		else if (digit3 == 2)
		digit3 = num2;
		else if (digit3 == 1)
		digit3 = num1;
		else
		digit3 = num0;
		
		while((TIFR1 & 0x02) == 0) {	//continue this loop for 1 second
			PORTD =	0b00000101; //first 7segment display
			PORTB = digit1;		//send the tens place
			_delay_ms(2);		//delay so data does not get sent to wrong 7segment
			 
			PORTD =	0b00000011; //second 7segment display
			PORTB = digit2;		//send the ones place
			PORTB |=(1<<PORTB7);//display the decimal point
			_delay_ms(2);		//delay so data does not get sent to wrong 7segment 
			
			PORTD =	0b00000110; //third 7segment display
			PORTB = digit3;		//send the decimal number
			_delay_ms(2);		//delay so data does not get sent to wrong 7segment 
		}			
		TIFR1 = 0x02;		//reset timer1
		
	}
}